package com.chlitina.store.modules.cash.model;

import com.chlitina.store.modules.cash.entity.StoreCardTimes;
import com.chlitina.store.modules.cash.model.recharge.CardTimesRecharge;

public class TimesCardPayment extends CardPayment {
	private int realTimes;// times in database
	private int rechargeTimes;// recharged times
	private int currentTimes;// current available times
	private int paidTimes;// need to pay
	private float costPertime;

	public TimesCardPayment(StoreCardTimes card, long id) {
		super(card, id);
		setNewCard(getCard().getId()==null);
		setRealTimes(card.getRemainTimes());
		setCurrentTimes(card.getRemainTimes());
		setRealMoney(card.getRemainMoney());
		setCurrentMoney(card.getRemainMoney());
		setCostPertime(card.getCostPerTime());
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(super.toString());
		sb.append(", realTimes="+getRealTimes());
		sb.append(", currentTimes="+getCurrentTimes());
		sb.append(", rechargeTimes="+getRechargeTimes());
		sb.append(", paidTimes="+getPaidTimes());
		sb.append(", costPertime="+getCostPertime());
		return sb.toString();
	}
	@Override
	public void reset() {
		super.reset();
		realTimes = getCard().getRemainTimes();
		currentTimes = realTimes;
		rechargeTimes = 0;
		costPertime = getCard().getCostPerTime();
		paidTimes = 0;
	}

	public StoreCardTimes getCard() {
		return (StoreCardTimes) card;
	}

	
	public void recharge(CardTimesRecharge recharge) {
		System.err.println("-----------");
		System.err.println("Recharge:" + recharge.toString());
		System.err.println("Before Recharge:" + toString());
		//
		int times = recharge.getTimes();
	    //更新充值的次数
		rechargeTimes+=times;
	    //更新可使用的最新次数
		currentTimes+=times;
	    //更新每次护理的所需的金额
	    costPertime=((realTimes*costPertime+recharge.getMoney())/currentTimes);
	    // FIXME money 是否需要被记录并更新
		System.err.println("End Recharge:" + toString());
	}
	
	/**
	 * remain times in the database
	 * 
	 * @return
	 */
	public int getRealTimes() {
		return realTimes;
	}

	private void setRealTimes(int realTimes) {
		this.realTimes = realTimes;
	}

	/**
	 * total recharged times in this shopping
	 * 
	 * @return
	 */
	public int getRechargeTimes() {
		return rechargeTimes;
	}

	public void setRechargeTimes(int rechargeTimes) {
		this.rechargeTimes = rechargeTimes;
	}

	public int getCurrentTimes() {
		return currentTimes;
	}

	public void setCurrentTimes(int currentTimes) {
		this.currentTimes = currentTimes;
	}

	/**
	 * total token times in this shopping
	 * 
	 * @return
	 */
	public int getPaidTimes() {
		return paidTimes;
	}

	public void setPaidTimes(int paidTimes) {
		this.paidTimes = paidTimes;
	}

	/**
	 * current cost per time
	 * 
	 * @return
	 */
	public float getCostPertime() {
		return costPertime;
	}

	public void setCostPertime(float costPertime) {
		this.costPertime = costPertime;
	}

	public void payTimes(int times) {
		//super.payMoney(getRealCost());
		paidTimes+=times;
		currentTimes -= times;
	}

	@Override
	public void payMoney() {
	}
}
